layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'table'], function () {
    var form = layui.form,
        table = layui.table,
        admin = layui.admin;

    form.render();
    form.verify({
        TwoDecimal: function (value) {
            if (!/^\d+(\.\d{1,2})?$/.test(value)) {
                return '最多输入两位小数';
            }
        },
        minMoney: function (value) {
            if (value<2) {
                return '金额太少';
            }
        }
    });

    initAccount();
    table.render({
        elem: '#withdraw_table'
        , url: '/withdrawManage/queryData'
        , method: "post"
        , height: 500
        , limit: 50							//显示数目
        , limits: [50, 100]
        , cols: [[
            {field: 'accountName', title: '开户人', width: 200, "align": "center"},
            {field: 'cardNo', title: '开户账号', width: 200, "align": "center"}
            , {
                field: 'succeed', width: 200, title: '状态', "align": "center", templet: function (d) {
                    var type = d.succeed;
                    if (type == '-11') {
                        return "正在处理中";
                    } else if (type == '00') {
                        return "提现成功";
                    } else if (type == '11') {
                        return "转账退回";
                    }
                }
            }
            , {
                field: 'amount', width: 200, title: '提现金额/元', "align": "center", templet: function (d) {
                    if(d.amount==null){
                        return 0;
                    }
                    return new Number(d.amount.toString()).div(new Number(100));
                }
            }
            , {
                field: 'actualAmount', width: 200, title: '扣费后金额/元', "align": "center", templet: function (d) {
                    if(d.actualAmount==null){
                        return new Number(d.amount.toString()).div(new Number(100));;
                    }
                    return new Number(d.actualAmount.toString()).div(new Number(100));
                }
            },
            {
                field: 'cost', width: 200, title: '税率及费用/元', "align": "center", templet: function (d) {
                    if(d.cost==null){
                        return 0;
                    }
                    return new Number(d.cost.toString()).div(new Number(100));
                }
            }

            , {field: 'createdTime', width: 200, title: '提现时间', "align": "center"}

        ]]
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {}
        , id: 'withdraw_table'
        , page: true
    });

    $(".search_btn").click(function () {
        var Succeed = $(".queryTable select[name='Succeed']").val();
        table.reload('withdraw_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                Succeed: Succeed
            }
        });
    });


    var flag = true;

    layui.use('form', function () {
        var form = layui.form;
        //监听提交
        form.on('submit(withdraw_btn)', function (data) {
            if (flag) {
                flag = false;
                //判断金额是否足够
                var act = new Number($('#account').val().toString());
                var amt = new Number($('#amount').val().toString());
                if (act >= amt) {
                    $.post("/withdrawManage/withdraw", data.field, function (result) {
                        flag = true;
                        if (result.code == 1) {

                            layer.msg("提现请求发起成功,等待后台处理");
                            setTimeout(function () {
                                window.location.reload();
                            }, 2000);
                        } else {
                            layer.msg(result.message)
                        }
                    });
                } else {
                    layer.msg('余额不足!!!');
                    flag = true;
                }
            } else {
                layer.msg('请不要重复提交!!!');
            }


            return false;
        });
    });



    //添加商户
    $("#withdraw_form_out").click(function(){

        /**
         * 弹出提现表单
         */

        admin.popup({
            title: '提现'
            ,shade: 0
            ,anim: -1
            ,offset:"20px"
            ,area: ['80%', '500px']
            ,skin: 'layui-anim layui-anim-upbit'
            ,content:$("#withdraw_form_none")
            ,end:function () {
                table.reload('withdraw_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {

                    }
                });
            }
        });

    });

});


/**
 * 页面加载完去查询用户余额
 */
function initAccount() {
    $.post(
        "/withdrawManage/account",
        {},
        function (result) {
            var count = new Number(result.account.toString()).div(new Number(100));
            $('#account').val(count);
        }
    )
}

//ajax异步验证码发送
$("#checkCode").click(function (el) {

    $.ajax({
        type: "POST", //提交方式
        url: "/safe/sendValidateCode",//路径
        data: {
            "bizType": "USER_INFO_MODIFY"
        },
        success: function (result) {//返回数据根据结果进行相应的处理

            if (result.code == 1) {

                layer.msg(result.message);

                //设置按钮不能被点击
                $("#checkCode").attr("disabled", true);
                //按钮30s秒倒计时
                var time = 30;
                var id = setInterval(function () {
                    if (time >= 1) {
                        time--;
                        $("#checkCode").html(time + "s后重新发送");
                    } else {
                        //倒计时结束按钮又可以再次被点击
                        clearInterval(id);
                        $("#checkCode").removeAttr("disabled", false).html("重新发送");
                    }
                }, 1000);


            } else {
                layer.msg(result.message);
            }
        }
    });

});